package repositories

import (
	"fmt"
	"gorm.io/gorm"
	"huanteng/web/global/application"

	"huanteng/vendors/paginate"
	"huanteng/web/models"
)



type MenuRepository struct {
}

func (a *MenuRepository)Test(){
	fmt.Println("caonima")
}
func (a *MenuRepository)AllMenus() []models.Menu {
	var list []models.Menu
	application.DB.Where("status=?",1).Order("parent_id asc").Order("sequence asc").Find(&list)
	//application.DB.Where("id = ?", id).Delete(&models.Admin{})
	return list
}
//删除菜单及关联数据
func (a *MenuRepository)Delete(ids []uint) error {
	//启用事物
	tx:=application.DB.Begin()

	//当出现异常程序恢复rollback
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	//批量删除
	for _,menuid :=range ids{
	//处理子节点数据
	if err := a.deleteMenuRecurve(tx, menuid); err != nil {
		tx.Rollback()
		return err
	}
	}
	//删除角色菜单关联表
	if err := tx.Where("menu_id in (?)", ids).Delete(&models.RoleMenu{}).Error; err != nil {
		tx.Rollback()
		return err
	}
	//删除菜单
	if err := tx.Where("id in (?)", ids).Delete(&models.Menu{}).Error; err != nil {
		tx.Rollback()
		return err
	}
	return tx.Commit().Error


	//application.DB.Where("id = ?", id).Delete(&models.Admin{})
}
func (a *MenuRepository)Update(menu *models.Menu)  {

	application.DB.Save(&menu)
}


func (a *MenuRepository)MenuList(menu *[]models.Menu,where models.Menu)  {

	application.DB.Where(where).Scopes(paginate.Paginate(&menu))
}
func (a *MenuRepository)FindById(id uint) (menu models.Menu)  {
	 application.DB.Where("id=?",id).First(&menu)
	 return
}
func (a *MenuRepository)Create(menu *models.Menu) error{
	return application.DB.Create(&menu).Error

}



// 获取菜单有权限的操作列表(查询前端code下面子节点菜单以及 管理员具有的角色 的菜单)
func (a *MenuRepository) GetMenuButton(adminsid uint, menuCode string, btns *[]string) (err error) {
	//通过查询的角色,关联角色菜单,
	sql := `select operate_type from menus
	      where status=1 and id in (
				select menu_id from role_menus where 
					menu_id in (
							select id from menus where parent_id in (
										select id from menus where code=?
										)
					)and role_id in (
							select role_id from admin_roles where admin_id=?
							)
			)`
	err = application.DB.Model(&models.Menu{}).Raw(sql, menuCode, adminsid).Pluck("operate_type", btns).Error
	return
}


// 获取管理员权限所有菜单
func (a *MenuRepository) GetMenuByAdminId(adminsid uint, menus *[]models.Menu) (err error) {
	//通过查询的角色,关联角色菜单,
	sql := `select * from menus
	      where status=1 and id in (
					select menu_id from role_menus where 
				  role_id in (select role_id from admin_roles where admin_id=?)
				)`
	err = application.DB.Raw(sql, adminsid).Find(menus).Error
	return
}

//删除我菜单下面节点
func (a *MenuRepository)deleteMenuRecurve(db *gorm.DB, parentID uint) error {
	where := models.Menu{}
	where.ParentID = parentID
	var menus []models.Menu
	dbslect := db.Where(&where)
	//查询当前子节点菜单
	if err := dbslect.Find(&menus).Error; err != nil {
		return err
	}

	//删除夫节点多对多关联表
	for _, menu := range menus {
		if err := db.Where("menu_id = ?", menu.ID).Delete(&models.RoleMenu{}).Error; err != nil {
			return err
		}
		if err := a.deleteMenuRecurve(db, menu.ID); err != nil {
			return err
		}
	}
	//子节点删除之后删除主节点
	if err := dbslect.Delete(&models.Menu{}).Error; err != nil {
		return err
	}
	return nil
}
